package com.teacher.pc.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.teacher.common.CommonResult;
import com.teacher.mapper.TermMapper;
import com.teacher.pojo.dto.classlevel.ClassLevelAddDto;
import com.teacher.pojo.dto.classlevel.ClassLevelGetDto;
import com.teacher.pojo.dto.classlevel.ClassLevelUpdateDto;
import com.teacher.pojo.entity.ClassLevel;
import com.teacher.pojo.vo.classlevel.ClassLevelGetVo;
import com.teacher.service.IClassLevelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 班级等级 前端控制器
 * </p>
 *
 * @author zfm
 * @since 2021-08-01
 */
@RestController
@RequestMapping("/pc/class-level")
@Api(tags = "班级等级")
public class ClassLevelController {
    @Autowired
    private IClassLevelService iClassLevelService;

    @Autowired
    private TermMapper termMapper;

    /**
     * 添加班级等级
     *
     * @param dto
     * @return
     */
    @ApiOperation("添加班级等级 classLevel_add 10->A等 20->B等 30->C等")
    @PostMapping
    @PreAuthorize("hasAuthority('classLevel_add')")
    public CommonResult<String> add(@RequestBody @Validated ClassLevelAddDto dto) {
        ClassLevel classLevel = new ClassLevel();
        dto.setTermId(termMapper.getCurrentTerm());
        BeanUtils.copyProperties(dto, classLevel);
        if (iClassLevelService.save(classLevel)) {
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    /**
     * 更新班级等级
     *
     * @param dto
     * @return
     */
    @ApiOperation("更新班级等级 classLevel_update")
    @PutMapping
    @PreAuthorize("hasAuthority('classLevel_update')")
    public CommonResult<String> update(@RequestBody @Validated ClassLevelUpdateDto dto) {
        ClassLevel classLevel = new ClassLevel();
        BeanUtils.copyProperties(dto, classLevel);
        if (iClassLevelService.updateById(classLevel)) {
            return CommonResult.success("更新成功");
        }
        return CommonResult.failed("更新失败");
    }

    /**
     * 删除班级等级
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除班级等级 classLevel_delete")
    @PreAuthorize("hasAuthority('classLevel_delete')")
    public CommonResult<String> del(@RequestBody List<Long> ids) {
        if (iClassLevelService.removeByIds(ids)) {
            return CommonResult.success("删除成功");
        }
        return CommonResult.failed("删除失败");
    }

    /**
     * 查询班级等级
     *
     * @param dto
     * @return
     */
    @GetMapping
    @ApiOperation("查询班级等级 classLevel_get")
    @PreAuthorize("hasAuthority('classLevel_get')")
    public CommonResult<IPage<ClassLevelGetVo>> getPage(ClassLevelGetDto dto) {
        return iClassLevelService.getPage(dto);
    }

    /**
     * 导入班级等级
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("导入班级等级 classLevel_upload")
    @PreAuthorize("hasAuthority('classLevel_upload')")
    public CommonResult<String> upload(@RequestParam(name = "file") MultipartFile file) {
        return iClassLevelService.upload(file);
    }
}

